package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.CouponActivity;
import com.example.mall.dto.CouponActivityDto;
import com.example.mall.mapper.CouponActivityMapper;
import com.example.mall.query.CouponActivityQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ICouponActivityService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import com.example.mall.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券活动表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class CouponActivityServiceImpl extends ServiceImpl<CouponActivityMapper, CouponActivity> implements ICouponActivityService {

    @Autowired
    private CouponActivityMapper couponActivityMapper;

    @Override
    public PageDTO<CouponActivityDto> queryPage(CouponActivityQuery couponActivityQuery) {
        if (couponActivityQuery == null) {
            couponActivityQuery = new CouponActivityQuery();
        }
        Page<CouponActivity> page = couponActivityQuery.toMpPageByDefaultCreateTimeAsc();
        Page<CouponActivity> cartPage = lambdaQuery()
                .like(couponActivityQuery.getActivityId()!=null, CouponActivity::getActivityId, couponActivityQuery.getActivityId())
                .like(StringUtils.isNotEmpty(couponActivityQuery.getActivityName()),CouponActivity::getActivityName,couponActivityQuery.getActivityName())
                .apply(couponActivityQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        couponActivityQuery.getStartTime())
                .apply(couponActivityQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        couponActivityQuery.getEndTime())
                .eq(StringUtils.isNotEmpty(couponActivityQuery.getStatus()),CouponActivity::getStatus,couponActivityQuery.getStatus())
                .orderByDesc(CouponActivity::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, CouponActivityDto.class);
    }

    @Override
    public void insert(CouponActivityDto couponActivityDto) {
        CouponActivity couponActivity = new CouponActivity();
        CopyUtils.copyProperties(couponActivityDto, couponActivity);
        couponActivity.setActivityId(CodeGenerateUtils.generateUniqueId());
        couponActivity.setCreateDate(new Date());
        couponActivity.setUpdateDate(new Date());
        couponActivity.setStatus(String.valueOf(BaseConstants.EFFECTIVE_STATUS));
        couponActivityMapper.insert(couponActivity);
    }

    @Override
    public void update(CouponActivityDto couponActivityDto) {
        AssertUtil.isNotNull(couponActivityDto.getActivityId(), "活动ID不能为空");
        CouponActivity couponActivity = couponActivityMapper.selectById(couponActivityDto.getActivityId());
        AssertUtil.isNotNull(couponActivity, "活动不存在");
        CopyUtils.copyProperties(couponActivityDto, couponActivity);
        couponActivity.setUpdateDate(new Date());
        couponActivityMapper.updateById(couponActivity);
    }

    @Override
    public void delete(Long activityId) {
        AssertUtil.isNotNull(activityId, "活动ID不能为空");
        CouponActivity couponActivity = couponActivityMapper.selectById(activityId);
        AssertUtil.isNotNull(couponActivity, "活动不存在");
        couponActivity.setStatus(String.valueOf(BaseConstants.LOSE_STATUS));
        couponActivity.setUpdateDate(new Date());
        couponActivityMapper.updateById(couponActivity);
    }

    @Override
    public void deleteBatch(List<Long> activityIds) {
        AssertUtil.isNotNull(activityIds, "活动ID列表不能为空");
        List<CouponActivity> couponActivities = couponActivityMapper.selectBatchIds(activityIds);
        AssertUtil.isNotNull(couponActivities, "活动不存在");
        for (CouponActivity couponActivity : couponActivities) {
            couponActivity.setStatus(String.valueOf(BaseConstants.LOSE_STATUS));
            couponActivity.setUpdateDate(new Date());
            couponActivityMapper.updateById(couponActivity);
        }
    }

    @Override
    public CouponActivity queryCouponActivityById(Long activityId) {
        AssertUtil.isNotNull(activityId, "活动ID不能为空");
        return couponActivityMapper.selectById(activityId);
    }

    @Override
    public List<CouponActivity> queryCouponActivityList() {
        return this.list();
    }
}
